import { defineStore } from 'pinia'
import {ref} from 'vue'
import {mergeCartAPI} from '@/api/cart.js'
import {cartStore} from '@/stores/index.js'
export const userStorey=defineStore('user',()=>{
  const userInfo=ref({})
  const setUser=async (val)=>{
    userInfo.value=val
    const cart=cartStore()
    let data=[]
    cart.cartList.forEach(item=>{
      data.push({skuId:item.skuId,selected:item.selected,count:item.count})
    })
    await mergeCartAPI(data)
    await cart.initCartList()
  }
  const removeUser=()=>{
    userInfo.value={}
  }
  return {
    userInfo,
    setUser,
    removeUser
  }
},
{
  persist: true,
})